/****************************************************
 * Description: Controllar for 用户组
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
	*  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuol.bfsuolframework.security.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bfsuol.bfsuolframework.core.controller.SpringControllerSupport;
import com.bfsuol.bfsuolframework.core.controller.json.MessageJson;
import com.bfsuol.bfsuolframework.core.controller.support.Pagination;
import com.bfsuol.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuol.bfsuolframework.core.exception.ValidationException;
import com.bfsuol.bfsuolframework.core.spring.QueryParameter;
import com.bfsuol.bfsuolframework.core.utils.StringUtils;
import com.bfsuol.bfsuolframework.security.annotations.*;
import com.bfsuol.bfsuolframework.security.dao.Group;
import com.bfsuol.bfsuolframework.security.dao.Role;
import com.bfsuol.bfsuolframework.security.service.GroupService;
import com.bfsuol.bfsuolframework.security.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/components/security/group")
public class GroupController extends SpringControllerSupport {

    @Autowired
    private GroupService groupService;
    @Autowired
    private RoleService roleService;

    @SecPrivilege(title="用户组管理")
	@RequestMapping("/index")
	public ModelAndView index(Model model){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("index"));
		return modelAndView;
	}

	@SecList
	@RequestMapping("/list")
	public ModelAndView list(
			@QueryParameter("query") QueryParam query,
			@ModelAttribute("page") Pagination page){

		com.baomidou.mybatisplus.extension.plugins.pagination.Page page1 = new Page(page.getDefaultCurrentPage(),page.getPageSize());
		com.baomidou.mybatisplus.extension.plugins.pagination.Page groupPages = groupService.page(page1,new QueryWrapper<>());
		page.setItems(groupPages.getRecords());
		page.setTotalRecord((int) groupPages.getTotal());

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("list"));
		return modelAndView;
	}
	@SecCreate
	@RequestMapping("/input")
	public ModelAndView create(@ModelAttribute Group group, Model model){
		
		List<Role> roles = roleService.list();
		model.addAttribute("roles", roles);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("input"));
		return modelAndView;
	}
	@SecEdit
	@RequestMapping("/input/{id}")
	public ModelAndView edit(@PathVariable("id") Long id, Model model){
		Group group = groupService.getById(id);
		model.addAttribute(group);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("input"));
		return modelAndView;
	}

	@SecCreate
	@SecEdit
	@RequestMapping("/save")
	public @ResponseBody MessageJson save(
			@RequestParam("roleIds") Long[] roleIds,
			@ModelAttribute Group group){
		
		if (StringUtils.isBlank(group.getTitle())) {// 验证用户名是否填写
			throw new ValidationException("请填写用户组名称！");
		}
		boolean flag = groupService.checkGroupTitle(group.getTitle(), group.getId());
		if (flag) { // 验证用户名的唯一性
			throw new ValidationException("用户组名称已存在！");
		}
		if(group.isNew()){
			groupService.save(group,roleIds);
		}else{
			Group g = groupService.getById(group.getId());
			g.setDescription(group.getDescription());
			g.setTitle(group.getTitle());
			groupService.updateById(g);
		}
		return MessageJson.information("保存成功");
	}
	
	@SecDelete
	@RequestMapping("/delete/{id}")
	public @ResponseBody MessageJson delete(@PathVariable("id") Long id){
		Group group = groupService.getById(id);
		if(group.getUsers().size()>0){
			throw new ValidationException("该用户组下有用户，不能删除");
		}
		groupService.deleteGroup(id);
		return MessageJson.information("成功删除1条");
	}

	@SecDelete
	@RequestMapping("/delete")
	public @ResponseBody MessageJson delete(@RequestParam("ids") Long[] ids){
		if(ids == null || ids.length == 0){
			throw new ValidationException("没有删除");
		}
		int count = 0;
		StringBuffer groupTitles = new StringBuffer();
		for(Long id : ids){
			Group group = groupService.getById(id);
			if(group.getUsers().size()>0){
				groupTitles.append(group.getTitle()+",");
			}else{
				groupService.deleteGroup(id);
				count++;
			}
		}
		String s = groupTitles.toString();
		if(s.indexOf(",")>0){
			s = s.substring(0,s.length()-1);
		}
		return MessageJson.information("成功删除"+count+"条,"+s+"用户组下有用户，不能删除");
	}
	
}
